package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.business.domain.SystemAccount;
import cn.wolfcode.p2p.business.mapper.SystemAccountMapper;
import cn.wolfcode.p2p.business.service.ISystemAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service@Transactional
public class SystemAccountServiceImpl implements ISystemAccountService {
    @Autowired
    private SystemAccountMapper systemAccountMapper;
    @Override
    public int save(SystemAccount systemAccount) {
        return systemAccountMapper.insert(systemAccount);
    }

    @Override
    public int update(SystemAccount systemAccount) {
        int count = systemAccountMapper.updateByPrimaryKey(systemAccount);
        if (count == 0) {
            System.out.println("乐观锁异常:systemAccountId:"+systemAccount);
            throw new DisplayableException("系统繁忙,请稍后再试");
        }
        return count;
    }

    @Override
    public SystemAccount get() {
        SystemAccount systemAccount = systemAccountMapper.selectCurrent();
        return systemAccount;
    }

    @Override
    public void initAccount() {
        if (this.get() == null) {
            this.save(new SystemAccount());
        }
    }
}
